package com.guldan.jaina.provider;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import com.guldan.core.enums.UploadRegisterTypeEn;
import com.guldan.core.enums.UploadTypeEn;
import com.guldan.core.redis.CacheService;
import com.guldan.jaina.dto.progress.BarDTO;
import com.guldan.jaina.dto.projectextensiondic.ProjectExtensionDicDTO;
import com.guldan.jaina.dto.projectregisterbatch.CopyBatchDataDTO;
import com.guldan.jaina.dto.projectregisterupload.ProjectRegisterUploadDTO;
import com.guldan.jaina.manager.*;
import com.guldan.jaina.utils.ProjectRegisterBatchDomainConvertUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.guldan.core.dubbo.client.ResponseDTO;
import com.guldan.core.search.Pagination;
import com.guldan.jaina.dto.citizeninfo.CitizenInfoSearchDTO;
import com.guldan.jaina.dto.orgbatchsummary.OrgBatchSummaryDTO;
import com.guldan.jaina.dto.orgbatchsummary.OrgBatchSummarySearchDTO;
import com.guldan.jaina.dto.projectregisterbatch.BatchAuditorDTO;
import com.guldan.jaina.dto.projectregisterbatch.ProjectRegisterBatchDTO;
import com.guldan.jaina.dto.projectregisterbatch.ProjectRegisterBatchSearchDTO;
import com.guldan.jaina.dto.register.RegisterTemplateDTO;
import com.guldan.jaina.dto.register.RegisterTemplateDataDTO;
import com.guldan.jaina.dto.register.RegisterTemplateSearchDTO;
import com.guldan.jaina.dto.register.RegisterUploadResultDTO;
import com.guldan.jaina.dto.register.summary.SummaryDTO;
import com.guldan.jaina.dto.register.summary.SummaryDetailDTO;
import com.guldan.jaina.dto.register.summary.SummaryFileDTO;
import com.guldan.jaina.dto.registerfail.RegisterFailDTO;
import com.guldan.jaina.dto.registerfail.RegisterFailSearchDTO;
import com.guldan.jaina.dto.registersuccess.RegisterDeleteDTO;
import com.guldan.jaina.dto.registersuccess.RegisterSuccessDTO;
import com.guldan.jaina.dto.registersuccess.RegisterSuccessSearchDTO;

/**
 * Created by Silas.
 * Date: 2016/12/3
 * Time: 17:33
 */
@Component
public class ProjectRegisterProviderImpl implements IProjectRegisterProvider {

    @Autowired
    private ProjectRegisterBatchManager projectRegisterBatchManager;

    @Autowired
    private RegisterSuccessManager registerSuccessManager;

    @Autowired
    private RegisterFailManager registerFailManager;

    @Autowired
    private RegisterManager registerManager;

    @Autowired
    private OrgBatchSummaryManager orgBatchSummaryManager;

    @Autowired
    private CacheService cacheService;

    @Autowired
    private ProjectRegisterUploadManager projectRegisterUploadManager;

    @Autowired
    private ProjectExtensionDicManager projectExtensionDicManager;

    @Override
    public ResponseDTO<ProjectRegisterBatchDTO> getProjectRegisterBatchDTO(ProjectRegisterBatchSearchDTO searchDTO) {
        ProjectRegisterBatchDTO dto = ProjectRegisterBatchDomainConvertUtils.convert(projectRegisterBatchManager.get(searchDTO.getBatchId()));
        ResponseDTO<ProjectRegisterBatchDTO> responseDTO = new ResponseDTO<>();
        responseDTO.setData(dto);
        return responseDTO;
    }

    @Override
    public ResponseDTO<OrgBatchSummaryDTO> getOrgBatchSummaryDTO(OrgBatchSummarySearchDTO searchDTO) {
        OrgBatchSummaryDTO orgBatchSummaryDTO = orgBatchSummaryManager.get(searchDTO.getSummaryId());
        ResponseDTO<OrgBatchSummaryDTO> responseDTO = new ResponseDTO<>();
        responseDTO.setData(orgBatchSummaryDTO);
        return responseDTO;
    }

    //乡镇登记人员
    //+++++++++++++++++++++++++++++++++++++++++
    //列表返回当前正在登记中的项目批次记录
    @Override
    public ResponseDTO<Pagination<ProjectRegisterBatchDTO>> townsRegistingList(ProjectRegisterBatchSearchDTO searchDTO) {
        ResponseDTO<Pagination<ProjectRegisterBatchDTO>> responseDTO = new ResponseDTO<>();
        responseDTO.setData(projectRegisterBatchManager.townsRegistingList(searchDTO));
        return responseDTO;
    }

    //提交登记上报
    @Override
    public ResponseDTO<Boolean> approvalSubmit(Integer batchId) {
        ResponseDTO<Boolean> responseDTO = new ResponseDTO<>();
        try {
            responseDTO.setData(projectRegisterBatchManager.approvalSubmit(batchId));
        } catch (Exception ex) {
            responseDTO.setStatus(false);
            responseDTO.setException(ex);
        }

        return responseDTO;
    }

    //列表查看所有登记失败人员的列表
    @Override
    public ResponseDTO<Pagination<RegisterFailDTO>> registFailList(RegisterFailSearchDTO searchDTO) {
        ResponseDTO<Pagination<RegisterFailDTO>> responseDTO = new ResponseDTO<>();
        responseDTO.setData(registerFailManager.registFailList(searchDTO));
        return responseDTO;
    }

    //列表查看所有失败人员的列表
    @Override
    public ResponseDTO<Pagination<RegisterSuccessDTO>> failList(RegisterSuccessSearchDTO searchDTO) {
        ResponseDTO<Pagination<RegisterSuccessDTO>> responseDTO = new ResponseDTO<>();
        responseDTO.setData(registerSuccessManager.failList(searchDTO));
        return responseDTO;
    }

    //成功人员列表
    @Override
    public ResponseDTO<Pagination<RegisterSuccessDTO>> successList(RegisterSuccessSearchDTO searchDTO) {
        ResponseDTO<Pagination<RegisterSuccessDTO>> responseDTO = new ResponseDTO<>();
        responseDTO.setData(registerSuccessManager.successList(searchDTO));
        return responseDTO;
    }

    //删除某个已经成功登记居民
    @Override
    public ResponseDTO<Boolean> deleteCitizen(RegisterDeleteDTO dto) {
        ResponseDTO<Boolean> responseDTO = new ResponseDTO<>();
        responseDTO.setData(registerSuccessManager.deleteCitizen(dto));
        return responseDTO;
    }

    //获取某个已经成功登记的居民信息
    @Override
    public ResponseDTO<RegisterSuccessDTO> getCitizen(Integer registerId) {
        ResponseDTO<RegisterSuccessDTO> responseDTO = new ResponseDTO<>();
        responseDTO.setData(registerSuccessManager.getCitizen(registerId));
        return responseDTO;
    }

    //保存某个已经成功登记的居民信息
    @Override
    public ResponseDTO<RegisterSuccessDTO> saveCitizen(RegisterSuccessDTO dto) {
        ResponseDTO<RegisterSuccessDTO> responseDTO = new ResponseDTO<>();
        responseDTO.setData(registerSuccessManager.saveCitizen(dto));
        return responseDTO;
    }

    //列表返回已经成功登记的居民信息
    @Override
    public ResponseDTO<Pagination<RegisterSuccessDTO>> listCitizen(RegisterSuccessSearchDTO searchDTO) {
        ResponseDTO<Pagination<RegisterSuccessDTO>> responseDTO = new ResponseDTO<>();
        responseDTO.setData(registerSuccessManager.listCitizen(searchDTO));
        return responseDTO;
    }

    //整体删除某个登记批次记录
    @Override
    public ResponseDTO<Boolean> deleteRegisterBatch(Integer batchId) {
        ResponseDTO<Boolean> responseDTO = new ResponseDTO<>();
        responseDTO.setData(projectRegisterBatchManager.deleteRegisterBatch(batchId));
        return responseDTO;
    }


    //乡镇登记已经提交上报的项目批次列表
    @Override
    public ResponseDTO<Pagination<ProjectRegisterBatchDTO>> townsRegistedList(ProjectRegisterBatchSearchDTO searchDTO) {
        ResponseDTO<Pagination<ProjectRegisterBatchDTO>> responseDTO = new ResponseDTO<>();
        responseDTO.setData(projectRegisterBatchManager.townsRegistedList(searchDTO));
        return responseDTO;
    }

    //列表返回已经完结的项目批次列表
    @Override
    public ResponseDTO<Pagination<ProjectRegisterBatchDTO>> finishBatchList(ProjectRegisterBatchSearchDTO searchDTO) {
        ResponseDTO<Pagination<ProjectRegisterBatchDTO>> responseDTO = new ResponseDTO<>();
        responseDTO.setData(projectRegisterBatchManager.finishBatchList(searchDTO));
        return responseDTO;
    }


    /*乡镇人员复核*/
    //+++++++++++++++++++++++++++++++++++++++++++++++
    //列表返回乡镇人员复核,当前需要操作的批次列表
    @Override
    public ResponseDTO<Pagination<ProjectRegisterBatchDTO>> townsAuditoringList(ProjectRegisterBatchSearchDTO searchDTO) {
        ResponseDTO<Pagination<ProjectRegisterBatchDTO>> responseDTO = new ResponseDTO<>();
        responseDTO.setData(projectRegisterBatchManager.townsAuditoringList(searchDTO));
        return responseDTO;
    }

    //列表返回乡镇人员复核,复核通过的批次列表
    @Override
    public ResponseDTO<Pagination<ProjectRegisterBatchDTO>> townsAuditoredList(ProjectRegisterBatchSearchDTO searchDTO) {
        ResponseDTO<Pagination<ProjectRegisterBatchDTO>> responseDTO = new ResponseDTO<>();
        responseDTO.setData(projectRegisterBatchManager.townsAuditoredList(searchDTO));
        return responseDTO;
    }

    //对批次操作，复核通过、复核不通过、上报
    @Override
    public ResponseDTO<Boolean> townsAuditorSubmit(BatchAuditorDTO dto) {
        ResponseDTO<Boolean> responseDTO = new ResponseDTO<>();
        responseDTO.setData(projectRegisterBatchManager.townsAuditorSubmit(dto));
        return responseDTO;
    }

    //对登记记录进行操作，复核通过、复核不通过
    @Override
    public ResponseDTO<Boolean> townsAuditorSubmitForRegister(BatchAuditorDTO dto) {
        ResponseDTO<Boolean> responseDTO = new ResponseDTO<>();
        responseDTO.setData(projectRegisterBatchManager.townsAuditorSubmitForRegister(dto));
        return responseDTO;
    }

    //列表查看某批次下待复核的登记记录列表
    @Override
    public ResponseDTO<Pagination<RegisterSuccessDTO>> townsAuditorWaitCizList(RegisterSuccessSearchDTO searchDTO) {
        ResponseDTO<Pagination<RegisterSuccessDTO>> responseDTO = new ResponseDTO<>();
        responseDTO.setData(registerSuccessManager.townsAuditorWaitCizList(searchDTO));
        return responseDTO;
    }

    /**
     * 下载待上报人员列表
     */
    @Override
    public ResponseDTO<List<RegisterTemplateDTO>> needReportingList(RegisterTemplateSearchDTO searchDTO) {
        ResponseDTO<List<RegisterTemplateDTO>> responseDTO = new ResponseDTO<>();
        responseDTO.setData(registerManager.needReportingList(searchDTO));
        return responseDTO;
    }

    //系统内新建登记批次
    @Override
    public ResponseDTO<BarDTO> createRegisterBatch(RegisterTemplateSearchDTO searchDTO) {
        ResponseDTO<BarDTO> responseDTO = new ResponseDTO<>();
        try {

            //获取项目扩展属性headers
            List<ProjectExtensionDicDTO> projectExtensionDicDTOs = projectExtensionDicManager.listByProjectId(searchDTO.getProjectId());
            String[] extHeaders = null;
            BigDecimal[] extValues = null;
            if (projectExtensionDicDTOs != null && projectExtensionDicDTOs.size() > 0) {
                extHeaders = new String[projectExtensionDicDTOs.size()];
                extValues = new BigDecimal[projectExtensionDicDTOs.size()];
                for (int i = 0; i < projectExtensionDicDTOs.size(); i++) {
                    extHeaders[i] = projectExtensionDicDTOs.get(i).getExtName();
                    extValues[i] = new BigDecimal(0.00);
                }
            }


            //获取需要导入的人员列表
            List<RegisterTemplateDataDTO> data = new ArrayList<>();
            List<RegisterTemplateDTO> list = registerManager.needReportingList(searchDTO);

            for (int i = 0; i < list.size(); i++) {
                RegisterTemplateDataDTO dto = new RegisterTemplateDataDTO();
                RegisterTemplateDTO d = list.get(i);

                dto.setProjectId(d.getProjectId());
                dto.setYear(d.getYear());
                dto.setSubsidyCount(d.getSubsidyCount());
                dto.setRegisterAreaName(d.getAreaName());
                dto.setAreaName(d.getAreaName());
                dto.setName(d.getName());
                dto.setIdentNumber(d.getIdentNumber());
                dto.setBankName(d.getBankName());
                dto.setBankNumber(d.getBankNumber());
                dto.setMoney(new BigDecimal(0.00));
                dto.setValues(extValues);
                dto.setProjectName(d.getProjectName());


                data.add(dto);
            }

            responseDTO.setData(registerManager.uploadCitizen(extHeaders, data, UploadRegisterTypeEn.System.getCode(), searchDTO.getBatchId()));
        } catch (Exception ex) {
            responseDTO.setStatus(false);
            responseDTO.setException(ex);
        }
        return responseDTO;

    }

    /**
     * 上传登记
     */
    @Override
    public ResponseDTO<BarDTO> uploadCitizen(String[] header, List<RegisterTemplateDataDTO> data) {
        ResponseDTO<BarDTO> responseDTO = new ResponseDTO<>();
        try {
            responseDTO.setData(registerManager.uploadCitizen(header, data, UploadRegisterTypeEn.Execl.getCode(), null));
        } catch (Exception ex) {
            responseDTO.setStatus(false);
            responseDTO.setException(ex);
        }
        return responseDTO;
    }

    //获取上传动作记录
    @Override
    public ResponseDTO<List<ProjectRegisterUploadDTO>> listUploadRecord(Integer batchId) {
        ResponseDTO<List<ProjectRegisterUploadDTO>> responseDTO = new ResponseDTO<>();
        try {
            responseDTO.setData(projectRegisterUploadManager.listUploadRecord(batchId));
        } catch (Exception ex) {
            responseDTO.setStatus(false);
            responseDTO.setException(ex);
        }
        return responseDTO;
    }

    /*主管部门审批*/
    //+++++++++++++++++++++++++++++++++++++++++++++++
    //主管部门,待审批批次列表
    @Override
    public ResponseDTO<Pagination<ProjectRegisterBatchDTO>> orgAuditorList(ProjectRegisterBatchSearchDTO searchDTO) {

        ResponseDTO<Pagination<ProjectRegisterBatchDTO>> responseDTO = new ResponseDTO<>();

        responseDTO.setData(projectRegisterBatchManager.orgAuditorList(searchDTO));
        return responseDTO;
    }

    //对批次进行审批，审批通过、审批不通过
    @Override
    public ResponseDTO<Boolean> orgAuditorApproval(BatchAuditorDTO dto) {
        ResponseDTO<Boolean> responseDTO = new ResponseDTO<>();
        responseDTO.setData(projectRegisterBatchManager.orgAuditorApproval(dto));
        return responseDTO;
    }

    //对登记人员记录进行审批，审批通过、审批不通过
    @Override
    public ResponseDTO<Boolean> orgAuditorApprovalForRegister(BatchAuditorDTO dto) {
        ResponseDTO<Boolean> responseDTO = new ResponseDTO<>();
        responseDTO.setData(projectRegisterBatchManager.orgAuditorApprovalForRegister(dto));
        return responseDTO;
    }

    //对批次进行汇总上报
    @Override
    public ResponseDTO<Boolean> orgAuditorSubmit(BatchAuditorDTO dto) {
        ResponseDTO<Boolean> responseDTO = new ResponseDTO<>();
        responseDTO.setData(projectRegisterBatchManager.orgAuditorSubmit(dto));
        return responseDTO;
    }

    //主管部门上报汇总后，汇总批次记录
    @Override
    public ResponseDTO<Pagination<OrgBatchSummaryDTO>> orgBatchSummaryList(OrgBatchSummarySearchDTO searchDTO) {
        ResponseDTO<Pagination<OrgBatchSummaryDTO>> responseDTO = new ResponseDTO<>();
        responseDTO.setData(orgBatchSummaryManager.orgBatchSummaryList(searchDTO));
        return responseDTO;
    }

    //获取某个批次下的登记成功人员记录
    @Override
    public ResponseDTO<Pagination<RegisterSuccessDTO>> orgAuditorRegisterList(RegisterSuccessSearchDTO searchDTO) {
        ResponseDTO<Pagination<RegisterSuccessDTO>> responseDTO = new ResponseDTO<>();
        responseDTO.setData(registerSuccessManager.orgAuditorRegisterList(searchDTO));
        return responseDTO;
    }


    /*区县财政审批*/
    //+++++++++++++++++++++++++++++++++++++++++++++++
    //财政待审批批次汇总记录
    @Override
    public ResponseDTO<Pagination<OrgBatchSummaryDTO>> financeAuditorList(OrgBatchSummarySearchDTO searchDTO) {
        ResponseDTO<Pagination<OrgBatchSummaryDTO>> responseDTO = new ResponseDTO<>();
        responseDTO.setData(orgBatchSummaryManager.financeAuditorList(searchDTO));
        return responseDTO;
    }

    //对批次汇总记录进行审批，审批通过、审批不通过
    @Override
    public ResponseDTO<Boolean> financeAuditorApproval(BatchAuditorDTO dto) {
        ResponseDTO<Boolean> responseDTO = new ResponseDTO<>();
        responseDTO.setData(orgBatchSummaryManager.financeAuditorApproval(dto));
        return responseDTO;
    }

    //查看某批次汇总下的登记成功人员记录
    @Override
    public ResponseDTO<Pagination<RegisterSuccessDTO>> financeAuditorRegisterList(RegisterSuccessSearchDTO searchDTO) {
        ResponseDTO<Pagination<RegisterSuccessDTO>> responseDTO = new ResponseDTO<>();
        responseDTO.setData(registerSuccessManager.financeAuditorRegisterList(searchDTO));
        return responseDTO;
    }

    //财政通过审批的批次汇总列表
    @Override
    public ResponseDTO<Pagination<OrgBatchSummaryDTO>> financeAuditoredSummaryBatchList(OrgBatchSummarySearchDTO searchDTO) {
        ResponseDTO<Pagination<OrgBatchSummaryDTO>> responseDTO = new ResponseDTO<>();
        responseDTO.setData(orgBatchSummaryManager.financeAuditoredSummaryBatchList(searchDTO));
        return responseDTO;
    }


    //生成代发文件
    @Override
    public ResponseDTO<SummaryFileDTO> financeCreatedFile(String summaryId) {
        ResponseDTO<SummaryFileDTO> responseDTO = new ResponseDTO<>();
        responseDTO.setData(registerManager.financeCreatedFile(summaryId));
        return responseDTO;
    }

    //生成代发和下载，修改状态用
    @Override
    public ResponseDTO<Boolean> updateSummaryState(int phase, String summaryId, String url) {
        ResponseDTO<Boolean> responseDTO = new ResponseDTO<>();
        responseDTO.setData(registerManager.updateSummaryState(phase, summaryId, url));
        return responseDTO;
    }

    //银行上传回执
    @Override
    public ResponseDTO<BarDTO> financeUploadReceipt(String summaryId, List<SummaryDetailDTO> detailList) {
        ResponseDTO<BarDTO> response = new ResponseDTO<>();
        try {
            response.setData(registerManager.financeUploadReceipt(summaryId, detailList));
        } catch (Exception ex) {
            response.setStatus(false);
            response.setException(ex);
        }
        return response;
    }

    //手机版
    @Override
    public ResponseDTO<List<RegisterSuccessDTO>> getRegisterList(CitizenInfoSearchDTO citizenInfoSearchDTO) {
        ResponseDTO<List<RegisterSuccessDTO>> response = new ResponseDTO<>();
        RegisterSuccessSearchDTO searchDTO = new RegisterSuccessSearchDTO();
        searchDTO.setIdentNumber(citizenInfoSearchDTO.getIdentNumber());
        searchDTO.setOrderByClause("create_time ASC");

        response.setData(registerSuccessManager.getRegisterSuccessList(searchDTO));
        return response;
    }

    //登记批次一键结转
    @Override
    public ResponseDTO<BarDTO> copyBatchData(CopyBatchDataDTO dataDTO) {
        ResponseDTO<BarDTO> response = new ResponseDTO<>();
        try {
            response.setData(projectRegisterBatchManager.copyBatchData2(dataDTO));
        } catch (Exception ex) {
            response.setStatus(false);
            response.setException(ex);
        }
        return response;
    }

}
